<?php

//aplicar a funcion html_entity_decode os demais atributos.


  // get the form inputs
  $nombre = get_input('nombre');
  $nombre = html_entity_decode($nombre, ENT_COMPAT, 'UTF-8');
  
  $descripcion = get_input('descripcion');
  $descripcion = html_entity_decode($descripcion, ENT_COMPAT, 'UTF-8');
  
  $tags = string_to_tag_array(get_input('tags'));
  $location = get_input('location');
  $acceso = get_input('acceso');
  $provincia = get_input('provincia');
  $tipoRoca = get_input('tipoRoca');
  
  if (get_input('climbia:spring')) {
  	$mejorEpoca['1'] = 'climbia:spring';
  }else{
  	$mejorEpoca['1'] = '';
  }
  if (get_input('climbia:summer')) {
  	$mejorEpoca['2'] = 'climbia:summer';
  }else{
  	$mejorEpoca['2'] = '';
  }
  if (get_input('climbia:fall')) {
  	$mejorEpoca['3'] = 'climbia:fall';
  }else{
  	$mejorEpoca['3'] = '';
  }
  if (get_input('climbia:winter')) {
  	$mejorEpoca['4'] = 'climbia:winter';
  }else{
  	$mejorEpoca['4'] = '';
  }
  
  if (get_input('climbia:sportclimbing')) {
  	$equipamiento['1'] = 'climbia:sportclimbing';
  }else{
  	$equipamiento['1'] = '';
  }
  if (get_input('climbia:bouldering')) {
  	$equipamiento['2'] = 'climbia:bouldering';
  }else{
  	$equipamiento['2'] = '';
  }
  if (get_input('climbia:traditional')) {
  	$equipamiento['3'] = 'climbia:traditional';
  }else{
  	$equipamiento['3'] = '';
  }
  if (get_input('climbia:artificial')) {
  	$equipamiento['4'] = 'climbia:artificial';
  }else{
  	$equipamiento['4'] = '';
  }
  
  $escuela_guid = get_input('escuela_guid');
  if ($escuela_guid){
  	$escuela = get_entity($escuela_guid);
  	$tipo = $escuela->getSubtype();
  	if (strcmp('escuela', $tipo)){
  		forward(REFERER);
  	}
  }else{
  
	  // crear un nuevo objeto escuela
	  $escuela = new ElggObject();
	  $escuela->subtype = "escuela";
	  $escuela->access_id = ACCESS_PUBLIC;
	  $escuela->title = $nombre;
	  $escuela->description = $descripcion;
	
	  	 
	  // for now make all blog posts public
	  $escuela->access_id = ACCESS_PUBLIC;
	 
	  // owner is logged in user
	  $escuela->owner_guid = 1;
	  //elgg_get_logged_in_user_guid();
	 
	  // save tags as metadata
	  $escuela->tags = $tags;
	 
	  // save to database and get id of the new escuela
	  $escuela_guid = $escuela->save();
  }
  $escuela->title = $nombre;
  $escuela->description = $descripcion;
  $escuela->location = $location;
  $escuela->acceso = $acceso;
  $escuela->provincia = $provincia;
  $escuela->tipoRoca = $tipoRoca;
  $escuela->spring = $mejorEpoca['1'];
  $escuela->summer = $mejorEpoca['2'];
  $escuela->fall = $mejorEpoca['3'];
  $escuela->winter = $mejorEpoca['4'];
  $escuela->sportclimbing = $equipamiento['1'];
  $escuela->bouldering = $equipamiento['2'];
  $escuela->traditional = $equipamiento['3'];
  $escuela->artificial = $equipamiento['4'];
  $escuela->save();
  
/////////////////////////////////////////////////////////////////////////////////////////
// Now see if we have a file icon
if ((isset($_FILES['icon'])) && (substr_count($_FILES['icon']['type'],'image/'))) {

	$icon_sizes = elgg_get_config('icon_sizes');

	$prefix = "escuelas/" . $escuela->guid;

	$filehandler = new ElggFile();
	$filehandler->owner_guid = $escuela->owner_guid;
	$filehandler->setFilename($prefix . ".jpg");
	$filehandler->open("write");
	$filehandler->write(get_uploaded_file('icon'));
	$filehandler->close();

	$thumbtiny = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['tiny']['w'], $icon_sizes['tiny']['h'], $icon_sizes['tiny']['square']);
	$thumbsmall = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['small']['w'], $icon_sizes['small']['h'], $icon_sizes['small']['square']);
	$thumbmedium = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['medium']['w'], $icon_sizes['medium']['h'], $icon_sizes['medium']['square']);
	$thumblarge = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['large']['w'], $icon_sizes['large']['h'], $icon_sizes['large']['square']);
	if ($thumbtiny) {

		$thumb = new ElggFile();
		$thumb->owner_guid = $escuela->owner_guid;
		$thumb->setMimeType('image/jpeg');

		$thumb->setFilename($prefix."tiny.jpg");
		$thumb->open("write");
		$thumb->write($thumbtiny);
		$thumb->close();

		$thumb->setFilename($prefix."small.jpg");
		$thumb->open("write");
		$thumb->write($thumbsmall);
		$thumb->close();

		$thumb->setFilename($prefix."medium.jpg");
		$thumb->open("write");
		$thumb->write($thumbmedium);
		$thumb->close();

		$thumb->setFilename($prefix."large.jpg");
		$thumb->open("write");
		$thumb->write($thumblarge);
		$thumb->close();

		$escuela->icontime = time();
	}
}
///////////////////////////////////////////////////////////////////////////////////////////
 
  // if the blog was saved, we want to display the new post
  // otherwise, we want to register an error and forward back to the form
  if ($escuela_guid) {
     system_message("Tu escuela ha sido guardada");
     forward($escuela->getURL());
  } else {
     register_error("La escuela no pudo ser guardada");
     forward(REFERER); // REFERER is a global variable that defines the previous page
  }
?>
